﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class baoming : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    private string wbh;
    protected void Page_Load(object sender, EventArgs e)
    {
        wbh = Session["wbh"].ToString();
        if ((db.proc_ISOpenDate(2)) < 1)
        {
            var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                          select p).ToList().First();

            int flag = Convert.ToInt32(result.proflag);
            if (flag > 1)
            {
                Response.Redirect("tj.aspx");
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('该功能暂未开放，请等待！');window.location='../../main.aspx'</script>");
                return;
            }
        }
        if (!IsPostBack == true)
        {
            ShowInfo();
        }
    }
    private void ShowInfo()
    {

        //var resNation = from p in db.proc_GetNation()
        //             select p;
        //var resPolitics = from p in db.proc_GetPolitics()
        //                select p;
        var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                      select p).ToList().First();

        int flag = Convert.ToInt32(result.proflag);
        if (flag > 1)
        {
            Response.Redirect("tj.aspx");
        }
        TextBox1.Text = result.sname;
        DropDownList1.SelectedValue = result.ssex;
        DropDownList2.SelectedValue = result.snation == null ? null : result.snation;
        string S = result.sbirth;
        if (S != null)
        {
            Label3.Text = S.Substring(0, 4) + "年" + S.Substring(4, 2) + "月" + S.Substring(6, 2) + "日"; 
        }
        Label4.Text = result.sidfy;
        DropDownList3.SelectedValue = result.sps;
        TextBox7.Text = result.gkbmh;
        TextBox8.Text = result.sdept;
        TextBox9.Text = result.byzy;
        if (result.schoolCode != "" && result.schoolCode != null)
        {
            DropDownList6.SelectedValue = result.schoolCode;
        }
        DropDownList5.SelectedValue = result.proNum;
        Label5.Text = result.proNum;
        TextBox13.Text = result.getaward;
        TextBox14.Text = result.advantage;
        TextBox15.Text = result.haddress;
        Label6.Text = result.sphone;
        TextBox17.Text = result.lxaddress;
        TextBox18.Text = result.postcode;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        int flag = Save();
        if (flag == -1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('保存失败，请重试！')</script>");
        }
        else if (flag == -2)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('保存失败，请填写完整信息！')</script>");
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('保存成功！')</script>");
        }
    }
    private int Save()
    {
        var result = (from p in db.proc_GetBkgxNowYear()
                      select p).ToList().First();
        string Syear = result.dYear;
        try
        {
            if (((db.proc_UpdateStuInfoBywbh(wbh, DropDownList5.SelectedValue, TextBox1.Text.Trim(), DropDownList1.SelectedValue, TextBox7.Text.Trim(), DropDownList6.SelectedValue, TextBox9.Text, TextBox18.Text.Trim(), TextBox17.Text.Trim(), TextBox14.Text, TextBox13.Text, DropDownList2.SelectedValue, DropDownList3.SelectedValue, DropDownList4.SelectedValue, TextBox15.Text.Trim(), Syear)) == -1) || ((db.proc_UpdateStuProFlagBywbh(wbh, 1)) == -1))
            {
                return -1;
            }
            db.SubmitChanges();

        }
        catch (Exception s)
        {
            return -1;
        }
        return 1;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int flag = Save();
        if (flag == -1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('保存失败，请重试！')</script>");
        }
        else if (flag == -2)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('保存失败，请填写完整信息！')</script>");
        }
        else
        {
            
            Response.Redirect("image_save.aspx");
        }
    }
}